package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    /**
     * 分页查询检查项
     * @param pageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        PageHelper.startPage(pageBean.getCurrentPage(), pageBean.getPageSize());
        Page<CheckItem> list = checkItemMapper.findPage(pageBean.getQueryString());

        PageResult result = new PageResult(list.getTotal(),list.getResult());
        return result;
    }

    /**
     * 查询所有检查项
     */
    @Override
    public List<CheckItem> findAll() {
        List<CheckItem> list = checkItemMapper.find(null);
        return list;
    }

    /**
     * 根据id查询检查项
     * @param id
     * @return
     */
    @Override
    public CheckItem findById(Integer id) {
        List<CheckItem> result = checkItemMapper.find(id);
        if(result!= null && result.size() != 0) {
            CheckItem item = result.get(0);
            return item;
        }
        else return null;
    }

    /**
     * 编辑检查项
     * @param item
     */
    @Override
    public void editCheckItem(CheckItem item) {
        checkItemMapper.editCheckItem(item);
    }

    /**
     * 根据检查组id查询关联的检查项id
     * @param checkGroupId
     * @return
     */
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer checkGroupId) {
        List<Integer> list = checkItemMapper.findCheckItemIdsByCheckGroupId(checkGroupId);
        return list;
    }

    /**
     * 根据id删除检查项
     * @param id
     */
    @Override
    public void deleteCheckItem(Integer id) {
        checkItemMapper.deleteById(id);
    }

    /**
     * 新增检查项
     * @param item
     */
    @Override
    public void addCheckItem(CheckItem item) {
        checkItemMapper.addCheckItem(item);
    }
}
